// ItemsArray.h: interface for the CItemsArray class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_ITEMSARRAY_H__F3685432_44F9_471A_853F_579267150E92__INCLUDED_)
#define AFX_ITEMSARRAY_H__F3685432_44F9_471A_853F_579267150E92__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <afxtempl.h>

template<class DATA, class ARG_DATA>
class CItemsArray : public CArray<DATA, ARG_DATA>
{
// Basics
public:
	CItemsArray()
		: m_strItemSeperator(',')
	{
	}
	virtual ~CItemsArray()
	{
	}
// Attributes
private:
	CString m_strItemSeperator;
// Operators
public:
	CItemsArray<DATA,ARG_DATA> & operator =(const CItemsArray<DATA,ARG_DATA> &arritems)
	{
		m_strItemSeperator = arritems.m_strItemSeperator;

		if (0 == GetSize())
			RemoveAll();
		for (int i= 0; i < arritems.GetSize(); i++)
		{
			Add( arritems[i] );
		}
		return (*this);
	}
// Functions
public:
	CItemsArray(const CString &strItems, const CString strItemSeperator =CString(','))
		: m_strItemSeperator(strItemSeperator)
	{
		const UINT nSeperatorLen= m_strItemSeperator.GetLength();
		int iItemStart= 0;
		int iItemEnd= 0;
		while (iItemStart < strItems.GetLength())
		{
			int iSeparatorPos = strItems.Find( m_strItemSeperator, iItemStart );
			iItemEnd = (-1==iSeparatorPos) ? strItems.GetLength() : iSeparatorPos;
			CString strItem(strItems.Mid( iItemStart, iItemEnd-iItemStart ));

			Add( DATA(strItem) );

			iItemStart = iItemEnd + nSeperatorLen;
		}
	}
	CItemsArray(const CArray<DATA,ARG_DATA> &arritems, const CString strItemSeperator =CString(','))
		: m_strItemSeperator(strItemSeperator)
	{
		for (int i= 0; i < arritems.GetSize(); i++)
		{
			Add( arritems[i] );
		}
	}
	void Merge(CArray<DATA,ARG_DATA> & arrMerged)
	{
		CArray<int,int&> arrMergedIndex;
			for (int iMergedItem= 0; iMergedItem < arrMerged.GetSize(); iMergedItem++)
			{
				BOOL ifSame= FALSE;
				for (int iAdmi= 0; iAdmi < this->GetSize(); iAdmi++)
				{
					if ((*this)[iAdmi] == arrMerged[iMergedItem])
					{
						ifSame = TRUE;
						break;
					}
				}
				if (FALSE == ifSame)
					arrMergedIndex.Add( iMergedItem );
			}
		for (int iMerged= 0; iMerged < arrMergedIndex.GetSize(); iMerged++)
		{
			int &iMergedIndex= arrMergedIndex[iMerged];
			this->Add( arrMerged[iMergedIndex] );
		}
	}
	CString GetSerialData() const
	{
		CString strSerialData;
		for (int i= 0; i < GetSize(); i++)
		{
			DATA &data= GetAt( i );
			strSerialData += data;
			if (i != GetSize()-1)
				strSerialData += m_strItemSeperator;
		}
	return (strSerialData);
	}
};

typedef CItemsArray<CString,LPCTSTR> CStringItemsArray;

template<class DATA, class ARG_DATA>
CString & operator +=(CString &str, const CItemsArray<DATA,ARG_DATA> &items)
{
	return (str += items.GetSerialData());
}

template<class DATA, class ARG_DATA>
CString operator +(const CString &str, const CItemsArray<DATA,ARG_DATA> &items)
{
	return (str + items.GetSerialData());
}

template<class DATA, class ARG_DATA>
CString operator +(const CItemsArray<DATA,ARG_DATA> &items, const CString &str)
{
	return (items.GetSerialData() + str);
}

#endif // !defined(AFX_ITEMSARRAY_H__F3685432_44F9_471A_853F_579267150E92__INCLUDED_)
